package com.itheima.sfbx.insurance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.sfbx.insurance.mapper.SafeguardMapper;
import com.itheima.sfbx.insurance.pojo.Safeguard;
import com.itheima.sfbx.insurance.service.ISafeguardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sfbx.insurance.dto.SafeguardVO;
import com.itheima.sfbx.insurance.enums.SafeguardEnum;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.framework.commons.utils.EmptyUtil;
import com.itheima.sfbx.framework.commons.utils.ExceptionsUtil;
import com.itheima.sfbx.framework.commons.exception.ProjectException;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
/**
 * @Description：保障项服务实现类
 */
@Slf4j
@Service
public class SafeguardServiceImpl extends ServiceImpl<SafeguardMapper, Safeguard> implements ISafeguardService {

    @Override
    public Page<SafeguardVO> findPage(SafeguardVO safeguardVO, int pageNum, int pageSize) {
        try {
            // 构建查询条件
            LambdaQueryWrapper<Safeguard> queryWrapper = queryWrapper(safeguardVO);
            // 构建分页对象
            Page<Safeguard> page = page(new Page<>(pageNum, pageSize), queryWrapper);
            // 构建返回对象
            Page<SafeguardVO> safeguardVOPage = BeanConv.toPage(page, SafeguardVO.class);
            return safeguardVOPage;
        } catch (Exception e) {
            log.error("保障项分页查询异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SafeguardEnum.PAGE_FAIL);
        }
    }

    @Override
    public SafeguardVO findById(String safeguardId) {
        return null;
    }

    @Override
    public SafeguardVO save(SafeguardVO safeguardVO) {

        try {
            // 将safeguardVO转换为safeguard对象
            Safeguard safeguard = BeanConv.toBean(safeguardVO, Safeguard.class);
            // 保存保障项
            boolean flag = save(safeguard);
            if (!flag) {
                throw new RuntimeException("保存保障项失败！");
            }
            // 返回safeguardVO
            return BeanConv.toBean(safeguard, SafeguardVO.class);
        } catch (Exception e) {
            log.error("保障项保存异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SafeguardEnum.SAVE_FAIL);
        }
    }

    @Override
    public Boolean update(SafeguardVO safeguardVO) {
        try {
            // 将safeguardVO转换为safeguard对象
            Safeguard safeguard = BeanConv.toBean(safeguardVO, Safeguard.class);
            // 更新保障项
            boolean flag = updateById(safeguard);
            if (!flag) {
                throw new RuntimeException("更新保障项失败！");
            }
            return flag;
        } catch (Exception e) {
            log.error("保障项更新异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SafeguardEnum.UPDATE_FAIL);
        }
    }

    @Override
    public Boolean delete(String[] checkedIds) {
        try {
            List<Long> idsLong = Arrays.stream(checkedIds).map(Long::parseLong).collect(Collectors.toList());
            boolean flag = removeByIds(idsLong);
            if (!flag){
                throw new RuntimeException("批量删除保障项失败");
            }
            return flag;
        } catch (RuntimeException e) {
            log.error("保障项删除异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SafeguardEnum.DEL_FAIL);
        }
    }

    @Override
    public List<SafeguardVO> findList(SafeguardVO safeguardVO) {
        try {
            // 构建查询条件
            LambdaQueryWrapper<Safeguard> queryWrapper = queryWrapper(safeguardVO);
            // 查询保障项列表
            List<Safeguard> safeguardList = list(queryWrapper);
            if (!EmptyUtil.isNullOrEmpty(safeguardList)) {
                // 将safeguardList转换为safeguardVOList
                List<SafeguardVO> safeguardVOList = BeanConv.toBeanList(safeguardList, SafeguardVO.class);
                return safeguardVOList;
            }
        } catch (Exception e) {
            log.error("保障项列表查询异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SafeguardEnum.LIST_FAIL);
        }
        return List.of();
    }

    private LambdaQueryWrapper<Safeguard> queryWrapper(SafeguardVO safeguardVO) {
        LambdaQueryWrapper<Safeguard> queryWrapper = new LambdaQueryWrapper<>();
        // 构建多条件查询
        queryWrapper.eq(!EmptyUtil.isNullOrEmpty(safeguardVO.getSafeguardKey()),
                Safeguard::getSafeguardKey, safeguardVO.getSafeguardKey());
        queryWrapper.like(!EmptyUtil.isNullOrEmpty(safeguardVO.getSafeguardKeyName()),
                Safeguard::getSafeguardKeyName, safeguardVO.getSafeguardKeyName());
        queryWrapper.eq(!EmptyUtil.isNullOrEmpty(safeguardVO.getDataState()),
                Safeguard::getDataState, safeguardVO.getDataState());
        return queryWrapper;
    }

    @Override
    public List<SafeguardVO> findShowPageItemByKey(List<String> safeguardKeyList) {
        return List.of();
    }

    @Override
    public SafeguardVO findBySafeguardKey(String safeguardKey) {
        return null;
    }
}
